import aes from 'crypto-js/aes';
import CryptoJS from 'crypto-js';
import secret from '../../../../../utils/secret';

const allRows = [
    {
        id: '11',
        name: '张三',
        employeeCode: '861100',
        year: 2010,
        month: 9,
        encryptTotalIncomeAmt: 1500000,
        encryptTotalNotaxAmt: 200000,
        encryptTotalTaxIncomeAmt: 1300000,
        encryptTotalPayedTaxAmt: 7000,
        encryptTotalFundOldAmt: 600,
        encryptTotalFundUnemployAmt: 600,
        encryptTotalFundHospitalAmt: 600,
        encryptTotalFundHouseAmt: 600,
        encryptTotalExpenseAmt: 20000,
        encryptTotalAfterTaxAmt: 0
    },
    {
        id: '12',
        name: '李四',
        employeeCode: '861101',
        year: 2010,
        month: 9,
        encryptTotalIncomeAmt: 1500000,
        encryptTotalNotaxAmt: 200000,
        encryptTotalTaxIncomeAmt: 1300000,
        encryptTotalPayedTaxAmt: 7000,
        encryptTotalFundOldAmt: 600,
        encryptTotalFundUnemployAmt: 600,
        encryptTotalFundHospitalAmt: 600,
        encryptTotalFundHouseAmt: 600,
        encryptTotalExpenseAmt: 20000,
        encryptTotalAfterTaxAmt: 0
    },
    {
        id: '13',
        name: '王五',
        employeeCode: '861102',
        year: 2010,
        month: 9,
        encryptTotalIncomeAmt: 1500000,
        encryptTotalNotaxAmt: 200000,
        encryptTotalTaxIncomeAmt: 1300000,
        encryptTotalPayedTaxAmt: 7000,
        encryptTotalFundOldAmt: 600,
        encryptTotalFundUnemployAmt: 600,
        encryptTotalFundHospitalAmt: 600,
        encryptTotalFundHouseAmt: 600,
        encryptTotalExpenseAmt: 20000,
        encryptTotalAfterTaxAmt: 0
    },
    {
        id: '14',
        name: '赵六',
        employeeCode: '861103',
        year: 2010,
        month: 9,
        encryptTotalIncomeAmt: 1500000,
        encryptTotalNotaxAmt: 200000,
        encryptTotalTaxIncomeAmt: 1300000,
        encryptTotalPayedTaxAmt: 7000,
        encryptTotalFundOldAmt: 600,
        encryptTotalFundUnemployAmt: 600,
        encryptTotalFundHospitalAmt: 600,
        encryptTotalFundHouseAmt: 600,
        encryptTotalExpenseAmt: 20000,
        encryptTotalAfterTaxAmt: 0
    },
    {
        id: '15',
        name: '钱七',
        employeeCode: '861104',
        year: 2010,
        month: 9,
        encryptTotalIncomeAmt: 1500000,
        encryptTotalNotaxAmt: 200000,
        encryptTotalTaxIncomeAmt: 1300000,
        encryptTotalPayedTaxAmt: 7000,
        encryptTotalFundOldAmt: 600,
        encryptTotalFundUnemployAmt: 600,
        encryptTotalFundHospitalAmt: 600,
        encryptTotalFundHouseAmt: 600,
        encryptTotalExpenseAmt: 20000,
        encryptTotalAfterTaxAmt: 0
    }
];

const getDecrypt = (params, secrets) =>
    JSON.parse(
        aes
            .decrypt(params, CryptoJS.enc.Utf8.parse(secrets), {
                iv: CryptoJS.enc.Utf8.parse(secrets),
                mode: CryptoJS.mode.CBC,
                padding: CryptoJS.pad.Pkcs7
            })
            .toString(CryptoJS.enc.Utf8)
    );

const search = request => {
    const params = getDecrypt(request.body.params, secret);
    const { page } = params;

    const startIndex = (page.pageNo - 1) * page.pageSize;
    const endIndex = page.pageNo * page.pageSize;
    const pages = Math.ceil(allRows.length / page.pageSize);
    console.log('params:', params);
    console.log('startIndex:', startIndex);
    console.log('endIndex:', endIndex);
    const rows = allRows.length <= startIndex ? null : allRows.slice(startIndex, endIndex);
    console.log(rows);

    return {
        code: 10000,
        data: {
            total: allRows.length,
            pages,
            pageNo: page.pageNo,
            pageSize: page.pageSize,
            rows
        }
    };
};

const normalServe = (req, res) => {
    setTimeout(() => {
        res.json({ code: 10000, data: [] });
    }, 2000);
}

const getTaxInit = (req, res) => {
    setTimeout(() => {
        res.json({ code: 10000, data: allRows[0] });
    }, 2000);
}

export default {
    'POST x/api/finance/salary/v1/salaryPayrollTax/queryTaxInitByPage': (req, res) => {
        res.json(search(req));
    },
    'GET x/api/finance/salary/v1/salaryPayrollTax/delTaxInit/*': (req, res) => {
        normalServe(req, res);
    },
    'GET x/api/finance/salary/v1/salaryPayrollTax/getTaxInit/*': (req, res) => {
        getTaxInit(req, res);
    }
};
